package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.eneity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.eneity.BaseAttrInfo;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
    implements BaseAttrInfoService{

    @Autowired
    BaseAttrValueService baseAttrValueService;

    /**
     * 根据分类id获取平台属性
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseAttrInfo> getBaseAttrAndValue(Long category1Id, Long category2Id, Long category3Id) {

        return baseMapper.getBaseAttrAndValue(category1Id,category2Id,category3Id);
    }

    /**
     * 保存平台属性
     * @param baseAttrInfo
     */
    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {

        //把属性名信息保存到base_attr_info
        boolean save = save(baseAttrInfo);
        Long attrId = baseAttrInfo.getId();
        //把属性值保存到base_attr_value
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        for (BaseAttrValue baseAttrValue : attrValueList) {

            baseAttrValue.setAttrId(attrId);
        }
        baseAttrValueService.saveBatch(attrValueList);
    }

    /**
     * 修改平台属性
     * @param baseAttrInfo
     */
    @Override
    public void updateAttrInfo(BaseAttrInfo baseAttrInfo) {

        //1.修改base_attr_info
        baseMapper.updateById(baseAttrInfo);
        List<BaseAttrValue> list = baseAttrInfo.getAttrValueList();
        List<Long> ids = new ArrayList<>();
        //删除，前端没提交的就是删除
        //delete * from base_attr_value where attr_id = ? and id not in (?,?,?);
        for (BaseAttrValue attrValue : list) {

            //获取前端传过来的attr_id
            if (attrValue.getId() != null){
                ids.add(attrValue.getId());
            }
        }
        if (ids.size() > 0){
            baseAttrValueService.lambdaUpdate()
                    .eq(BaseAttrValue::getAttrId,baseAttrInfo.getId())
                    .notIn(BaseAttrValue::getId,ids).remove();
        }else {
            baseAttrValueService.lambdaUpdate()
                    .eq(BaseAttrValue::getAttrId,baseAttrInfo.getId());
        }


        //2.修改base_attr_value
        for (BaseAttrValue attrValue : list) {

            if (attrValue.getId() == null){
                //如果属性值id为空，则是新增,新增本来就有attr_id，所有要回填
                attrValue.setAttrId(baseAttrInfo.getId());
                baseAttrValueService.save(attrValue);
            }else {
                //如果属性值id不为空，则是修改
                baseAttrValueService.updateById(attrValue);
            }
        }

    }
}




